﻿package com.saumya.audio
{
	import org.waterford.audio.AudioController;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.events.Event;
	
	/**
	 * This is just a demo class showing how to use the audio controller.
	 * @author Saumya
	 * @version 0.1
	 */
	
	public class MyTestAudioController extends AudioController
	{
		private var _audioID:String = '';
		public static var INSTRUCTION_COMPLETE:String="instruction audio complete"
		
		public function MyTestAudioController(name:String) 
		{
			trace(this, ' : MyTestAudioController : MyTestAudioController : ', name);
			super(name);
		}
		
		override public function addLoadables():void {
			trace(this, ' : MyTestAudioController : addLoadables : ');
			
			this.audioLoader.add('assets/audio/gameOne/bg.mp3', { id:"bg" } );//audio : bg
			this.audioLoader.add('assets/audio/gameOne/1.mp3', { id:"startAudioOne" } );
			this.audioLoader.add('assets/audio/gameOne/2.mp3', { id:"startAudioTwo" } );
			this.audioLoader.add('assets/audio/gameOne/3.mp3', { id:"startAudioThree" } );
			this.audioLoader.add('assets/audio/gameOne/4.mp3', { id:"startAudioFour" } );
			this.audioLoader.add('assets/audio/gameOne/5.mp3', { id:"startAudioFive" } );
			this.audioLoader.add('assets/audio/gameOne/6.mp3', { id:"startAudioSix" } );
			this.audioLoader.add('assets/audio/gameOne/7.mp3', { id:"startAudioSeven" } );
			this.audioLoader.add('assets/audio/gameOne/8.mp3', { id:"startAudioEight" } );
			this.audioLoader.add('assets/audio/gameOne/9.mp3', { id:"startAudioNine" } );
			this.audioLoader.add('assets/audio/gameOne/10.mp3', { id:"startAudioTen" } );
			this.audioLoader.add('assets/audio/gameOne/11.mp3', { id:"startAudioEleven" } );
			this.audioLoader.add('assets/audio/gameOne/12.mp3', { id:"startAudioTwelve" } );
			this.audioLoader.add('assets/audio/gameOne/13.mp3', { id:"roStart" } );
			this.audioLoader.add('assets/audio/gameOne/14.mp3', { id:"roHelp" } );
			this.audioLoader.add('assets/audio/gameOne/15.mp3', { id:"roBack" } );
			this.audioLoader.add('assets/audio/gameOne/16.mp3', { id:"roRestart" } );
			
			this.audioLoader.add('assets/audio/gameOne/click.mp3', { id:"click" } );
			this.audioLoader.add('assets/audio/gameOne/correct.mp3', { id:"correct" } );
			this.audioLoader.add('assets/audio/gameOne/incorrect.mp3', { id:"incorrect" } );
			
			//last call
			super.addLoadables();
		}
		
		override public function playAudio(audioId:String = ''):void {
			this.audioID = audioId;
			var audio:Sound = this.audioLoader.getSound(audioId);
			this.soundChannel = audio.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE,onAudioComplete);
		}
		
		/**
		 * Plays the starting audio at the beginning of the game play
		 */
		public function playIntroduction():void {
			this.audioID = 'startAudioOne';
			var audio:Sound = this.audioLoader.getSound(this.audioID);
			this.soundChannel = audio.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE,onAudioPlayComplete);
		}
		
		/**
		 * Plays the game intro audio
		 */
		public function playGamePlayIntro():void {
			this.audioID = 'startAudioTwelve';
			var audio:Sound = this.audioLoader.getSound(this.audioID);
			this.soundChannel = audio.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE,onAudioPlayComplete);
		}
		
		/**
		 * Plays Rollover Start audio
		 */
		public function playRoStart():void {
			this.playAudio('roStart');
		}
		/**
		 * Plays Rollover Help audio
		 */
		public function playRoHelp():void {
			this.playAudio('roHelp');
		}
		/**
		 * Plays Rollover Back audio
		 */
		public function playRoBack():void {
			this.playAudio('roBack');
		}
		/**
		 * Plays Rollover Re-Start audio
		 */
		public function playRoRestart():void {
			this.playAudio('roRestart');
		}
		/**
		 * Plays the click sound
		 */
		public function playClickSound():void {
			this.playAudio('click');
		}
		/**
		 * Plays the audio for correct answer
		 */
		public function playCorrectAnswerSound():void {
			this.playAudio('correct');
		}
		/**
		 * Plays the audio for incorrect answer
		 */
		public function playWrongAnswerSound():void {
			this.playAudio('incorrect');
		}
		/**
		 * Plays the audio for background
		 */
		public function playBgSound():void {
			//this.playAudio('bg');
			var audio:Sound = this.audioLoader.getSound('bg');
			this.soundBgChannel = audio.play();
			this.soundBgChannel.addEventListener(Event.SOUND_COMPLETE,onBgAudioPlayComplete);
		}
		
		private function onBgAudioPlayComplete(e:Event):void 
		{
			trace(this,' : onBgAudioPlayComplete : ');
			this.playBgSound();
		}
		 
		/**
		 * TODO :
		 * @param	e
		 */
		private function onAudioPlayComplete(e:Event):void 
		{
			trace(this, ' : onAudioPlayComplete : ');
			dispatchEvent(new Event(Mmg01AudioController.INSTRUCTION_COMPLETE))
		}
		
		/**
		 * Stops all the sounds except bg sound
		 */
		public function stopAllSoundsExceptBg():void {
			this.stopGameSounds();
		}
		
		/**
		 * TODO : Getter
		 */
		public function get audioID():String { return _audioID; }
		/**
		 * TODO : Setter
		 */
		public function set audioID(value:String):void 
		{
			_audioID = value;
		}
		
	}
	
}